package com.mdd.front.service.impl.zdjd;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.zdjd.TZdjdAgent;
import com.mdd.common.entity.zdjd.param.DeviceFeedbackDataParam;
import com.mdd.common.mapper.zdjd.TZdjdAgentMapper;
import com.mdd.common.util.ToolUtils;
import com.mdd.front.service.zdjd.ITZdjdAgentService;
import com.mdd.front.validate.common.PageValidate;
import com.mdd.front.validate.zdjd.TZdjdAgentCreateValidate;
import com.mdd.front.validate.zdjd.TZdjdAgentSearchValidate;
import com.mdd.front.validate.zdjd.TZdjdAgentUpdateValidate;
import com.mdd.front.vo.zdjd.TZdjdAgentDetailVo;
import com.mdd.front.vo.zdjd.TZdjdAgentListedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 代理商实现类
 * @author LikeAdmin
 */
@Service
public class TZdjdAgentServiceImpl implements ITZdjdAgentService {
        
    @Resource
    TZdjdAgentMapper tZdjdAgentMapper;

    /**
     * 代理商列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TZdjdAgentListedVo>
     */
    @Override
    public PageResult<TZdjdAgentListedVo> list(PageValidate pageValidate, TZdjdAgentSearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<TZdjdAgent> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        tZdjdAgentMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:name:str",
            "=:phone:str",
            "=:idCard@id_card:str",
            "=:deviceTotalAmount@device_total_amount:int",
            "=:echargeTotalAmount@echarge_total_amount:long",
            "=:promotionTotalAmount@promotion_total_amount:int",
        });

        IPage<TZdjdAgent> iPage = tZdjdAgentMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TZdjdAgentListedVo> list = new LinkedList<>();
        for(TZdjdAgent item : iPage.getRecords()) {
            TZdjdAgentListedVo vo = new TZdjdAgentListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 代理商详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return TZdjdAgent
     */
    @Override
    public TZdjdAgentDetailVo detail(Integer id) {
        TZdjdAgent model = tZdjdAgentMapper.selectOne(
                new QueryWrapper<TZdjdAgent>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TZdjdAgentDetailVo vo = new TZdjdAgentDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 代理商新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TZdjdAgentCreateValidate createValidate) {
        TZdjdAgent model = new TZdjdAgent();
        model.setName(createValidate.getName());
        model.setPhone(createValidate.getPhone());
        model.setIdCard(createValidate.getIdCard());
        model.setDeviceTotalAmount(createValidate.getDeviceTotalAmount());
        model.setEchargeTotalAmount(createValidate.getEchargeTotalAmount());
        model.setPromotionTotalAmount(createValidate.getPromotionTotalAmount());
        tZdjdAgentMapper.insert(model);
    }

    /**
     * 代理商编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TZdjdAgentUpdateValidate updateValidate) {
        TZdjdAgent model = tZdjdAgentMapper.selectOne(
                new QueryWrapper<TZdjdAgent>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setName(updateValidate.getName());
        model.setPhone(updateValidate.getPhone());
        model.setIdCard(updateValidate.getIdCard());
        model.setDeviceTotalAmount(updateValidate.getDeviceTotalAmount());
        model.setEchargeTotalAmount(updateValidate.getEchargeTotalAmount());
        model.setPromotionTotalAmount(updateValidate.getPromotionTotalAmount());
        tZdjdAgentMapper.updateById(model);
    }

    /**
     * 代理商删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        TZdjdAgent model = tZdjdAgentMapper.selectOne(
                new QueryWrapper<TZdjdAgent>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        tZdjdAgentMapper.delete(new QueryWrapper<TZdjdAgent>().eq("id", id));
    }

    /**
     * 根据代理商key查询代理商信息
     * @param agentKey
     * @return
     */
    @Override
    public TZdjdAgent selectAgentByKey(String agentKey) {
        TZdjdAgent agent = tZdjdAgentMapper.selectOne(new QueryWrapper<TZdjdAgent>().eq("agent_key", agentKey).last("limit 1"));
        return Optional.ofNullable(agent).orElse(new TZdjdAgent());
    }

    /**
     * 新增代理商
     * @param agent
     */
    @Override
    public void insertAgent(TZdjdAgent agent) {
        tZdjdAgentMapper.insert(agent);
    }

    @Override
    public List<TZdjdAgent> selectAll() {
        return Optional.ofNullable(tZdjdAgentMapper.selectList(new QueryWrapper<TZdjdAgent>())).orElse(new ArrayList<>());
    }

}
